package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.transaction.Transactional;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IVideoDao;
import com.magicode.cms.dao.IVideoListDao;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.entiy.base.VideoList;
import com.magicode.cms.service.IVideoListService;
import com.magicode.cms.service.IVideoService;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.exception.BusinessException;
@Service("servicevideolist")
@Transactional
public class VideoListServiceImpl extends BaseServiceImpl<VideoList, IVideoListDao> implements IVideoListService{
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.lastModifiedDate desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("name", ""+entity_alias_name+".name ||'.'||"+entity_alias_name+".extension  as name ");
//			gridsel.put("videoSize", "size("+entity_alias_name+".videoes) as videoSize ");
			gridsel.put("size", "("+entity_alias_name+".size/(1024*1024)) as size ");
			gridsel.put("lastModifiedDate", sql_date_method+"("+entity_alias_name+".lastModifiedDate,'"+sql_time_format+"' )  as lastModifiedDate ");
			gridsel.put("path", ""+entity_alias_name+".path as path ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end as valid ");
			gridsel.put("isExists", "case when "+entity_alias_name+".isExists = 1 then '存在' else '不存在' end as isExists ");
			gridsel.put("isDelete", "case when "+entity_alias_name+".isDelete = 1 then '待删除' else '正常' end as isDelete ");
			
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql =" from " +VideoList.class.getName()+" t  ";
		if(null!=map && !map.isEmpty()){
			if(null!=map && !map.isEmpty()){
				hql += "where 1 = 1 ";
				if(map.containsKey(field_id)){
					hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
				}
				if(map.containsKey("name")){
					hql = hql+" and "+entity_alias_name+".name like '%"+map.get("name")+"%'";
				}
				if(map.containsKey("extension")){
					hql = hql+" and "+entity_alias_name+".extension like '%"+map.get("extension")+"%'";
				}
				
				if(map.containsKey("beginLastModifiedDate")){
//					 sql_date_method+"("+entity_alias_name+".subcribeTime  ,'"+sql_time_format+"' ) 
					hql = hql+" and "+sql_date_method+"("+entity_alias_name+".lastModifiedDate,'"+sql_date_format+"' )  >=  "
							+ " '"+map.get("beginLastModifiedDate")+"' ";
				}
				if(map.containsKey("endLastModifiedDate")){
//					 sql_date_method+"("+entity_alias_name+".subcribeTime  ,'"+sql_time_format+"' ) 
					hql = hql+" and "+sql_date_method+"("+entity_alias_name+".lastModifiedDate,'"+sql_date_format+"' )  <=  "
							+ " '"+map.get("endLastModifiedDate")+"' ";
				}
				if(map.containsKey("absolutePath")){
					hql = hql+" and "+entity_alias_name+".absolutePath like '%"+map.get("absolutePath")+"%'";
				}	
				if(map.containsKey("path")){
					hql = hql+" and "+entity_alias_name+".path like '%"+map.get("path")+"%'";
				}					
				if(map.containsKey("valid") && map.get("valid") != null && map.get("valid").trim().length() > 0){
					hql = hql+" and "+entity_alias_name+".valid = '"+map.get("valid")+"'";
				}
				if(map.containsKey("isExists") && map.get("isExists") != null && map.get("isExists").trim().length() > 0){
					hql = hql+" and "+entity_alias_name+".isExists = '"+map.get("isExists")+"'";
				}
				if(map.containsKey("isDelete") && map.get("isDelete") != null && map.get("isDelete").trim().length() > 0){
					hql = hql+" and "+entity_alias_name+".isDelete = '"+map.get("isDelete")+"'";
				}
				if(map.containsKey("videoTitle")){
					hql = hql+" and "+entity_alias_name+".id in ( "
							+ "select  v.videoList.id  from "+Video.class.getName()+" v where v.title like '%"+map.get("videoTitle")+"%' "
							+ ") ";
				}
//				查找有视频数据
				if(map.containsKey("videoSize")){
					String videoSize = map.get("videoSize");
					if(videoSize != null ){
						if("0".equals(videoSize)){
							hql = hql+" and size("+entity_alias_name+".videoes) = 0 ";
						}
						else{
							hql = hql+" and size("+entity_alias_name+".videoes) > 0 ";
						}
					}
				}
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "name");
		map.put(json_title, "文件名");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "path");
		map.put(json_title, "路径");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "size");
		map.put(json_title, "文件大小(M)");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "lastModifiedDate");
		map.put(json_title, "最后修改日期");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "isExists");
		map.put(json_title, "是否存在");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "isDelete");
		map.put(json_title, "是否删除");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "videoSize");
		map.put(json_title, "关联视频数量");
		map.put("width", 70);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public IVideoListDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return vedioListDao;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Boolean deleteObject(String objid)  {
		// TODO Auto-generated method stub
		try {
			VideoList obj =this.getObject(objid);
			
			if(obj.getIsDelete() == 1 && obj.getIsExists() == 0 ){
				if(null!=obj){
					return  this.getDao().deleteObject(obj);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return false;
		
	}
	
	
	@Override
	public List<JsonOperation> getOpts(Map<String,Object> pram,String url,Map<String,Object> rowdata){
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		String id = null;
		Integer isExist = 0;
		Integer isDelete = 0;
		
		try {
			id = rowdata.get("id").toString();
			isExist = (Integer)rowdata.get("isExist");
			isDelete = (Integer)rowdata.get("isDelete");
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(pram != null && pram.containsKey("multiSelect") && "1".equals(pram.get("1")) ){
			
		}
		else{
			opt.setName("修改");
			opt.setUrl(ctx.getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
			opt.setType("edit");
			opts.add(opt);
//			如果待删除，并且不存在，将可删除该文件
			if(isExist == 0 && isDelete == 1){
				opt= new JsonOperation();
				opt.setName("删除");
				opt.setUrl(ctx.getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
				opt.setType("delete");
				opts.add(opt);
			}
		}
		return opts;
//		return getOpts(pram,url, rowdata.get("id").toString());
	}
	@Override
	public List<JsonOperation> getOpts(String url,String id){
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete");
		opts.add(opt);
		return opts;
	}
	

	@Autowired
	@Qualifier("daovideolist")
	private IVideoListDao vedioListDao;

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	public List getVideoList(){
		String hql = " from com.magicode.cms.entiy.base.VideoList ro ";
		List<VideoList> list = this.commonDaoService.getListByHQL(hql);
		return list;
	}
	
	
	@Autowired
	private ServletContext ctx;

	public ServletContext getCtx() {
		return ctx;
	}
	public void setCtx(ServletContext ctx) {
		this.ctx = ctx;
	}
	public IVideoListDao getVedioListDao() {
		return vedioListDao;
	}
	public void setVedioListDao(IVideoListDao vedioListDao) {
		this.vedioListDao = vedioListDao;
	}
	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}
	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}
}
